package com.itheima.service;

import com.itheima.mapper.ShoppingMapper;
import com.itheima.pojo.Shopping;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * @author 林晓伟
 */
@Service
public class ShoppingService {
    @Resource
    private ShoppingMapper shoppingMapper;

    /**
     * F05-购物车列表
     */
    public List<Shopping> all() {
        return shoppingMapper.all();
    }

    /**
     * F04-加入购物车
     */
    public Shopping add(Shopping shopping) {
        List<Shopping> nd = shoppingMapper.nd();
        if (nd != null) {
            List<Integer> chas = shoppingMapper.chas(shopping.getName());
            for (Integer cha : chas) {
                shopping.setNumber(cha + 1);
            }
            shoppingMapper.ab(shopping);
            Shopping cha = shoppingMapper.cha(shopping.getName());
            if (cha != null) {
                return cha;
            }
        }
        long l = System.currentTimeMillis();
        String s = String.valueOf(l);
        shopping.setId(s);
        shopping.setNumber(1);
        shoppingMapper.add(shopping);
        return shoppingMapper.a(shopping.getId());
    }

    /**
     * F06-清空购物车
     */
    public Integer delete() {
        return shoppingMapper.delete();
    }

    /**
     * F07-删除购物车商品
     */
    public Integer newdelete(String dishId, String setmealId) {
        return shoppingMapper.newDelete(dishId, setmealId);

    }
}
